<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">
        当前位置：首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>服务卡管理</el-breadcrumb-item>
      <el-breadcrumb-item>服务卡发放记录</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区 -->
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="queryInfo.query"
        size="mini"
      >
        <el-form-item label="发放时间">
          <el-col :span="11">
            <el-form-item prop="grant_time_start">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="queryInfo.query.grant_time_start"
                size="mini"
                style="width: 125px"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
            <el-form-item prop="grant_time_end">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="queryInfo.query.grant_time_end"
                size="mini"
                style="width: 125px"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="发放渠道" prop="channel">
          <el-select
            v-model="queryInfo.query.channel"
            placeholder="请选择"
            filterable
            size="mini"
            clearable
          >
            <el-option key="1" label="小程序" value="1"> </el-option>
            <el-option key="2" label="第三方渠道" value="2"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发放人" prop="grant_uid">
          <el-select
            v-model="queryInfo.query.grant_uid"
            placeholder="请选择"
            filterable
            size="mini"
            clearable
          >
            <el-option
              v-for="item in selectData.user"
              :key="item.id"
              :label="item.name + '【' + item.id + '】'"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关键字" prop="keyword">
          <el-input
            v-model="queryInfo.query.keyword"
            placeholder="请输入手机号、服务卡名称或编号"
            style="width: 210px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="theme-background-color"
            size="mini"
            @click="searchList"
          >
            搜索
          </el-button>
          <el-button size="mini" @click="resetSearchForm"> 重置 </el-button>
        </el-form-item>
      </el-form>
      <!-- 操作区域 -->
      <div>
        <el-button
          icon="el-icon-download"
          size="mini"
          :loading="btnLoading"
          @click="downloadExcel"
        >
          导出数据
        </el-button>
      </div>
      <!-- 用户列表区域 -->
      <el-table :data="list" border stripe>
        <!-- <el-table-column label="序号" type="index"></el-table-column> -->
        <el-table-column label="批次" prop="batch"></el-table-column>
        <el-table-column
          label="发放流水号"
          prop="main_serial_number"
          min-width="120px"
        ></el-table-column>
        <el-table-column
          label="发放时间"
          prop="grant_time"
          min-width="90px"
        ></el-table-column>
        <el-table-column
          label="手机号码"
          prop="mobile"
          min-width="100px"
        ></el-table-column>
        <el-table-column
          label="服务卡名称【ID】"
          min-width="150px"
          max-width="300px"
        >
          <template slot-scope="scope">
            {{ scope.row.card_config_name }}【{{ scope.row.card_config_id }}】
          </template>
        </el-table-column>
        <el-table-column label="发放数量" prop="quantity"></el-table-column>
        <el-table-column
          label="发放渠道"
          prop="channel"
          min-width="90px"
        ></el-table-column>
        <el-table-column
          label="发放人"
          prop="grant_name"
          min-width="150px"
          max-width="300px"
        ></el-table-column>
        <el-table-column label="发放状态" prop="status.name"></el-table-column>
        <el-table-column
          label="备注"
          prop="remark"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column label="操作" width="100px" fixed="right">
          <template slot-scope="scope">
            <el-link type="primary" @click="changeTab(scope.row.id)">
              查看明细
            </el-link>
            <br />
            <!-- <el-link
              type="primary"
              v-if="scope.row.status.id == 0"
              @click=""
            >
              再次发放
            </el-link> -->
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: {
          grant_time_start: '',
          grant_time_end: '',
          channel: '',
          grant_uid: '',
          keyword: ''
        },
        pagenum: 1,
        pagesize: 20
      },
      selectData: [],
      list: [],
      total: 0,
      btnLoading: false
    }
  },
  created() {
    this.getSelectData()
    this.initDate()
    this.getListData()
  },
  methods: {
    async getSelectData() {
      const { data: res } = await this.$http.post('/admin/data/find', {
        code: 'user'
      })
      // 返回结果
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.selectData = res.data
    },
    initDate() {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      this.queryInfo.query.grant_time_start = this.getFormatDate(start)
      this.queryInfo.query.grant_time_end = this.getFormatDate(end)
    },
    searchList() {
      this.queryInfo.pagenum = 1
      this.getListData()
    },
    resetSearchForm() {
      //   console.log(this)
      this.$refs.searchFormRef.resetFields()
      this.getListData()
    },
    async getListData() {
      // 返回数据重命名为res
      const { data: res } = await this.$http.post(
        '/admin/service_card_grant_record/list',
        this.queryInfo
      )
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.list = res.data.list
      this.total = res.data.total
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      //   console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getListData()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      //   console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getListData()
    },
    async downloadExcel() {
      this.btnLoading = true
      this.$http({
        method: 'post',
        url: '/admin/service_card_grant_record/export',
        data: this.queryInfo.query,
        headers: {
          token: window.sessionStorage.getItem('token')
        },
        responseType: 'blob'
      })
        .then((res) => {
          this.btnLoading = false
          if (res.data.type) {
            // 文件下载
            const blob = new Blob([res.data], {
              type: 'application/vnd.ms-excel'
            })
            let link = document.createElement('a')
            link.href = URL.createObjectURL(blob)
            link.setAttribute('download', '服务卡发放记录数据.xlsx')
            link.click()
            link = null
            this.$message.success('导出成功')
          } else {
            // 返回json
            this.$message.warning(res.data.msg)
          }
        })
        .catch((err) => {
          this.btnLoading = false
          this.$message.error(err)
        })
    },
    changeTab(id) {
      console.log(id)
      const path = '/servicecard/QueryList'
      this.$router.push({ path: path, query: { record_id: id } })
      this.$emit('updateActivePath', path)
      window.sessionStorage.setItem('activePath', path)
    }
  }
}
</script>

<style lang="less" scoped>
.line {
  text-align: center;
}
</style>
